#include "fVoxelsAnalysis.h"
#include "ui_fVoxelsAnalysis.h"
#include "widget.h"
#include "CVtkImageCmp.h"
#include <QFileDialog>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkAxesActor.h>
#include <vtkPolyDataMapper.h>
#include <QVTKOpenGLWidget.h>
#include <vtkProperty.h>
#include <vtkGenericOpenGLRenderWindow.h>
#include <QtConcurrent/QtConcurrent>
fVoxelsAnalysis::fVoxelsAnalysis(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::fVoxelsAnalysis)
{
    ui->setupUi(this);
    connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(onAnalysis()));
}

fVoxelsAnalysis::~fVoxelsAnalysis()
{
    delete ui;
}



void fVoxelsAnalysis::onAnalysis()
{
    QString path=QFileDialog::getOpenFileName(this,tr("analysis path"),".");
    if(path.isEmpty())
    {
        qDebug()<<"path is empty.";return;
    }
    QtConcurrent::run(this,&fVoxelsAnalysis::onInternaleAnalysis,path,QStringList()<<"png"<<"jpg");
    QEventLoop el;
    connect(this,SIGNAL(sFinished()),&el,SLOT(quit()));
    el.exec();

    vtkSmartPointer<vtkActor> actor=vtkActor::New();
    actor->SetMapper(_mapper);
    double actorColor[]={0,5,0.2,0.3};
    actor->GetProperty()->SetColor(actorColor);
    Widget*win=static_cast<Widget*>(this->parentWidget());
    win->_renderer->AddActor(actor);

//    vtkSmartPointer<vtkAxesActor> axes=vtkAxesActor::New();
//    win->_renderer->AddViewProp(axes);
    win->updateVtk();

}

void fVoxelsAnalysis::onInternaleAnalysis(const QString &path, const QStringList &filters)
{
    CVtkImageCmp ic(path,filters);
    ic.analysis();
    _mapper=ic.result;
    emit sFinished();
}




